package com.zilla.android.zillacore.libzilla.util;

import android.text.TextUtils;
import com.zilla.android.zillacore.libzilla.file.SharedPreferenceService;

/**
 * 用户账号管理
 * Created by chenze on 13-12-4.
 */
public class UserManager {

    private static UserManager userManager = null;

    private UserManager() {

    }

    public static UserManager getInstance() {
        if (userManager == null) {
            userManager = new UserManager();
        }
        return userManager;
    }

    public String getUserName() {
        return SharedPreferenceService.getInstance().get("userName", "");
    }

    public void saveUserName(String userName) {
        SharedPreferenceService.getInstance().put("userName", userName);
    }

    public String getPassword() {
        return SharedPreferenceService.getInstance().get("password", "");
    }

    public void savePassword(String password) {
        SharedPreferenceService.getInstance().put("password", password);
    }

    /**
     * 是否登录
     *
     * @return
     */
    public boolean isLogin() {
        String userName = getUserName();
        String psw = getPassword();
        if (TextUtils.isEmpty(userName) || TextUtils.isEmpty(psw)) {
            return false;
        }
        return true;
    }

    public void clearCache() {
        SharedPreferenceService.getInstance().put("userName", "");
        SharedPreferenceService.getInstance().put("password", "");
    }

    /**
     * 获取Author
     * @return
     */
    public String getAuthor() {
        return "Basic " + Util.base64_encode(getUserName() + ":" + getPassword()).trim();
    }
}
